package persistence;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import model.CategoryAttribute;

public class CategoryAttributeDAO extends BaseDAO {

	public CategoryAttributeDAO() throws Exception{}
	
	public int deleteAllCategoryAttribute() throws SQLException{
		int retorno;
		String sql = "DELETE FROM category_attribute";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		retorno = ps.executeUpdate();
		closeConnection();
		return retorno;  
	}
	
	public boolean insertCategoryAttribute(CategoryAttribute categoryAttribute, boolean closeConnection) throws SQLException{
		boolean retorno;
		String sql = "INSERT INTO category_attribute(category_id, attribute_id) values (?,?)";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		ps.setInt(1, categoryAttribute.getCategoryId());
		ps.setInt(2, categoryAttribute.getAttributeId());
		retorno = ps.executeUpdate() == 1;
	    if(closeConnection){
	    	closeConnection();
	    }
	    return retorno;
	}
	
	public int insertListCategoryAttribute(List<CategoryAttribute> listCategoryAttribute) throws Exception{
		int ret=0;
		for (int i = 0; i < listCategoryAttribute.size(); i++) {
			boolean foiInserido;
			foiInserido = insertCategoryAttribute(listCategoryAttribute.get(i),false);
			if(foiInserido){
				ret++;
			}
		}
		closeConnection();
		return ret;
	}
}
